import uuid
import os

##This Script prepare static file for oss server
# it will
# 1. regenerate names of all ti image file
# 2. copy them to a new directory

def get_conf_file(path):
    print('path=', path)
    f=''
    for item in os.listdir(path):
        if '.xls' in item  or  '.xls' in item:
            f=item
            break
    return   f

book_dir=''
out_path=''


conf_file=get_conf_file(book_dir)
ISBN=os.path.splitext(conf_file)[0]


book_name=os.path.split(book_dir)[-1]



xx=os.path.join(out_path, ISBN, 'data' )
if not os.path.exists(xx):
    os.makedirs(xx)     ###create all directories once


#--1. copy configure files (cover and .xls) -------
dst=os.path.join(out_path, ISBN)
for f in os.listdir(book_dir):
    if f=='data':
        continue
    src = os.path.join(book_dir, f)
    if os.path.isdir(src):
        continue

    os.system('cp ' + src +' '+ dst )

#--2. copy ti images
img_dir= os.path.join(book_dir, 'data')
xx = os.path.join(out_path, ISBN, 'data')
for f in os.listdir(img_dir):
    if '_' in f:
        continue        ##ignore image not processed
    src = os.path.join(book_dir, f)
    dst=os.path.join(xx, uuid.uuid1()+os.path.splitext(f)[-1])
    os.system('cp ' + src + ' ' + dst)